home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d11 / frasrc14.arc / GIFVIEW.C < prev    next >
Text File  |  1990-08-02  |  6KB  |  234 lines

  1. /*
  2.  * This GIF decoder is designed for use with Bert Tyler's FRACTINT
  3.  * program. It should be noted that the "FRACTINT" program only decodes
  4.  * GIF files FRACTINT creates, so this decoder code lacks full generality
  5.  * in the following respects: supports single image, non-interlaced GIF
  6.  * files with no local color maps and no extension blocks.
  7.  *
  8.  * GIF and 'Graphics Interchange Format' are trademarks (tm) of
  9.  * Compuserve, Incorporated, an H&R Block Company.
  10.  *
  11.  *                                            Tim Wegner
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <dos.h>
  17.  
  18. #include "fractint.h"
  19.  
  20. #define MAXCOLORS    256
  21.  
  22. struct RGB
  23. {
  24.    unsigned char red, green, blue;
  25. };
  26.  
  27. extern int decoder();
  28. extern int rowcount;                    /* row counter for screen */
  29. extern char readname[];                 /* file name        */
  30. static FILE *fpin = NULL;                /* FILE pointer         */
  31. unsigned int height;
  32.  
  33. extern    FILE *dacfile;
  34. extern    char MAP_name[];
  35. extern    int    mapset;
  36.  
  37. extern int glassestype;
  38. extern int display3d;
  39. extern int dotmode;                    /* so we can detect disk-video */
  40. extern int calc_status;
  41. extern long calctime;
  42. extern long timer_interval;
  43.  
  44. int bad_code_count = 0;                 /* needed by decoder module */
  45.  
  46. get_byte()
  47. {
  48.    static int c;
  49.  
  50.    /* make sure fpin NULL if not open */
  51.    if (!fpin)
  52.    {
  53.       char temp1[81];
  54.  
  55.       strcpy(temp1,readname);
  56.       if (strchr(temp1,'.') == NULL) {
  57.      strcat(temp1,DEFAULTFRACTALTYPE);
  58.      if ((fpin = fopen(temp1,"rb")) != NULL) {
  59.         fclose(fpin);
  60.         }
  61.      else {
  62.         strcpy(temp1,readname);
  63.         strcat(temp1,ALTERNATEFRACTALTYPE);
  64.         }
  65.      }
  66.  
  67.       if ((fpin = fopen(temp1, "rb")) == NULL)
  68.      return (-1);
  69.    }
  70.    return (fgetc(fpin)); /* EOF is -1, as desired */
  71. }
  72.  
  73. extern unsigned char dacbox[256][3];    /* Video-DAC (filled in by SETVIDEO) */
  74. extern int reallyega;            /* "really-an-ega" flag */
  75. extern int paletteVGA[16];        /* VGA Palette-to-DAC registers */
  76. extern unsigned char decoderline[2049]; /* write-line routines use this */
  77.  
  78. /* Main entry decoder */
  79. gifview()
  80. {
  81.    unsigned numcolors;
  82.    unsigned char buffer[16];
  83.    unsigned width, finished;
  84.  
  85.    int status;
  86.    int i, j, k, planes;
  87.  
  88.    status = 0;
  89.  
  90.    /* initialize the row count for write-lines */
  91.    rowcount = 0;
  92.  
  93.    /* zero out the full write-line */
  94.    for (width = 0; width < 2049; width++) decoderline[width] = 0;
  95.  
  96.    /* Get the screen description */
  97.    for (i = 0; i < 13; i++)
  98.    {
  99.       if ((buffer[i] = (unsigned char)get_byte ()) < 0)
  100.       {
  101.      close_file();
  102.      return(-1);
  103.       }
  104.    }
  105.  
  106.    if(strncmp(buffer,"GIF87a",3) ||             /* use updated GIF specs */
  107.       buffer[3] < '0' || buffer[3] > '9' ||
  108.       buffer[4] < '0' || buffer[4] > '9' ||
  109.       buffer[5] < 'A' || buffer[5] > 'z' )
  110.    {
  111.       close_file();
  112.       return(-1);
  113.    }
  114.  
  115.    planes = (buffer[10] & 0x0F) + 1;
  116.  
  117.    if((buffer[10] & 0x80)==0)     /* color map (better be!) */
  118.    {
  119.       close_file();
  120.       return(-1);
  121.    }
  122.    numcolors = 1 << planes;
  123.  
  124.    for (i = 0; i < numcolors; i++)
  125.    {
  126.       if (numcolors == 16 && !reallyega)    /* straight copy or indirect via palette? */
  127.      k = paletteVGA[i];
  128.       else
  129.      k = i;
  130.       for (j = 0; j < 3; j++) {
  131.      if ((buffer[j] = (unsigned char)get_byte()) < 0)
  132.         return(-1);
  133.      if (dacbox[0][0] != 255)    /* (only if we really have a DAC) */
  134.         if(!display3d || (glassestype != 1 && glassestype != 2))
  135.            dacbox[k][j] = buffer[j] >> 2;
  136.       }
  137.    }
  138.    /* don't read if glasses */
  139.    if (mapset && glassestype!=1 && glassestype != 2)
  140.    {
  141.        dacfile = fopen(MAP_name,"r");
  142.        ValidateLuts(dacfile);  /* read the palette file */
  143.        fclose(dacfile); /* close it */
  144.        if (dotmode != 11)
  145.       spindac(0,1); /* load it, but don't spin */
  146.    }
  147.    if (dacbox[0][0] != 255 && dotmode != 11)
  148.       spindac(0,1);      /* update the DAC */
  149.  
  150.    if (dotmode == 11) /* disk-video */
  151.    {
  152.        movecursor(2,0);
  153.        printf("...restoring...");
  154.    }
  155.  
  156.    /* Now display one or more GIF objects */
  157.    finished = 0;
  158.    while (!finished)
  159.    {
  160.       switch (get_byte())
  161.       {
  162.       case ';':
  163.      /* End of the GIF dataset */
  164.  
  165.      finished = 1;
  166.      status = 0;
  167.      break;
  168.  
  169.     case '!':                               /* GIF Extension Block */
  170.         get_byte();            /* read (and ignore) the ID */
  171.         while ((i = get_byte()) > 0)    /* get the data length */
  172.             for (j = 0; j < i; j++)
  173.                 get_byte();    /* flush the data */
  174.         break;
  175.       case ',':
  176.      /*
  177.       * Start of an image object. Read the image description.
  178.       */
  179.  
  180.       for (i = 0; i < 9; i++)
  181.       {
  182.          if ((buffer[i] = (unsigned char)get_byte ()) < 0)
  183.          {
  184.         status = -1;
  185.         break;
  186.          }
  187.       }
  188.       if(status < 0)
  189.       {
  190.          finished = 1;
  191.          break;
  192.       }
  193.  
  194.       width  = buffer[4] | buffer[5] << 8;
  195.       height = buffer[6] | buffer[7] << 8;
  196.  
  197.      /* Setup the color palette for the image */
  198.  
  199.      if (calc_status == 1) /* should never be so, but make sure */
  200.         calc_status = 0;
  201.      status = timer(decoder,1,width);
  202.      if (calc_status == 1) /* e.g., set by line3d */
  203.      {
  204.         calctime = timer_interval; /* note how long it took */
  205.         if (keypressed() != 0)
  206.            calc_status = 3; /* interrupted, not resumable */
  207.         else
  208.            calc_status = 4; /* complete */
  209.      }
  210.      finished = 1;
  211.      break;
  212.       default:
  213.      status = -1;
  214.      finished = 1;
  215.      break;
  216.       }
  217.    }
  218.    close_file();
  219.    if (dotmode == 11) /* disk-video */
  220.    {
  221.        home();
  222.        printf("Restore completed        \n\n                    ");
  223.    }
  224.  
  225.    /* if (status == 0)
  226.       buzzer(0); */        /* audible signal - we done */
  227.    return(status);
  228. }
  229. close_file()
  230. {
  231.    fclose(fpin);
  232.    fpin = NULL;
  233. }
  234.